﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GDD2_Game3
{
    class GameUpdate : IUpdate
    {
        private Game1 game1;
		private GameData gameData;

		public GameUpdate(Game1 g1, GameData data)
        {
			game1 = g1;
			gameData = data;
        }

        public void update(GameTime gameTime)
        {
			game1.setScreenDimensions(Convert.ToInt32(gameData.ScreenDimensions.X), Convert.ToInt32(gameData.ScreenDimensions.Y));
            //moving vehicle
			gameData.Vehicle.move(leftHeld(), rightHeld(), upHeld(), downHeld(), gameData.Track.Walls, gameData.Track.Finish, Convert.ToSingle(gameTime.ElapsedGameTime.TotalSeconds));
			gameData.Vehicle.keepOnTrack(gameData.Track.Dimensions);
			foreach(Wall wall in gameData.Track.Walls)
			{
				wall.update(gameData.Vehicle);
			}
			foreach(FinishLine fl in gameData.Track.Finish)
			{
				fl.update(gameData.Vehicle);
			}
			for(int i = 0; i < gameData.Track.PowerUps.Count; ++i)
			{
				string message = gameData.Track.PowerUps[i].update(gameData.Vehicle, gameData.Track);
				if(message != null)
				{
					gameData.PreviousEffectMessage = message;
				}
			}
			if(!gameData.IsFinished && gameData.Vehicle.IsFinished)
			{
				gameData.GameTime += gameTime.TotalGameTime.TotalSeconds;
				gameData.IsFinished = true;
			}
        }

		private bool leftHeld()
		{
			return Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A);
		}

		private bool rightHeld()
		{
			return Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D);
		}

		private bool upHeld()
		{
			return Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W);
		}

		private bool downHeld()
		{
			return Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S);
		}
    }
}
